﻿using System;
using System.Collections.Generic;

using Microsoft.AspNetCore.Mvc;

using Xc.DataAccess.Core;
using Xc.DataAccess.DataBase.DeviceInfos;
using Xc.DataAccess.DataBase.DeviceInfos.Models;
using System.Linq;
using Xc.DataAccess.Protocoals.WaterMeters.Models;

namespace Xc.WaterMeters.ServerHosts.Controllers
{
    /// <summary>
    /// 设备信息管理服务
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeviceInfoController : ControllerBase
    {
        private readonly IDbDeviceInfoService dbDeviceInfoService;

        public DeviceInfoController(IDbDeviceInfoService dbDeviceInfoService)
        {
            this.dbDeviceInfoService = dbDeviceInfoService;
        }
        /// <summary>
        /// 查询设备信息
        /// </summary>
        /// <param name="dateTimeStart"></param>
        /// <param name="dateTimeEnd"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<IEnumerable<DeviceInfo>> Get(DateTime? dateTimeStart,DateTime? dateTimeEnd)
        {
              var datas= dbDeviceInfoService.Get(s =>
              {
                  if (dateTimeStart.HasValue)
                  {
                    s=  s.Where(prop=> prop.CreateDateTime>dateTimeStart);
                  }
                  if (dateTimeEnd.HasValue)
                  {
                      s = s.Where(p => p.CreateDateTime <= dateTimeEnd);
                  }
                  return s;
              });
            return datas;
        }
        /// <summary>
        /// 更新设备信息
        /// </summary>
        /// <param name="deviceInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Update([FromBody]DeviceInfoAdd[] deviceInfo)
        {
            return dbDeviceInfoService.Update(deviceInfo);
        }
        /// <summary>
        /// 添加并更新设备信息
        /// </summary>
        /// <param name="deviceInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddUpdate([FromBody] DeviceInfoAdd[] deviceInfo)
        {
            return dbDeviceInfoService.AddUpdate(deviceInfo);
        }
        /// <summary>
        /// 添加新设备
        /// </summary>
        /// <param name="deviceInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Add([FromBody] DeviceInfoAdd[] deviceInfo)
        {
            return dbDeviceInfoService.Add(deviceInfo);
        }
        /// <summary>
        /// 删除设备信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Delete(string deviceId)
        {
            return dbDeviceInfoService.Delete(deviceId);
        }
        /// <summary>
        /// 彻底删除设备信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result PhysicallyDelete(string deviceId)
        {
            return dbDeviceInfoService.PhysicallyDelete(deviceId);
        }
        [HttpGet]
        public ResultData<DeviceInfo[]> DeletedDevice()
        {
            return dbDeviceInfoService.DeletedDevice();
        }
    }
}
